package com.mf.morefun.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mf.morefun.R;
import com.mf.morefun.bean.RankBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RankAdapter extends BaseAdapter {
    private Context mContext;
    private List<RankBean> mRankList;

    public RankAdapter(Context mContext, List<RankBean> mRankList) {
        this.mContext = mContext;
        this.mRankList = mRankList;
    }

    @Override
    public int getCount() {
        return mRankList.size();
    }

    @Override
    public Object getItem(int position) {
        return mRankList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (null == convertView) {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_rank_item, null);
        } else {
            view = convertView;
        }
        if (null != mRankList && mRankList.size() > 0){
            ViewHolder holder = new ViewHolder(view);
            if (position == 0){
                holder.llBg.setBackgroundResource(R.color.nomalbg_color);
            }else if(position == (mRankList.size() - 1)){
                if ((position % 2) == 0){
                    holder.llBg.setBackgroundResource(R.color.nomalbg_color);
                }else {
                    holder.llBg.setBackgroundResource(R.drawable.white_bottomshape);
                }
            }else {
                if ((position % 2) == 0){
                    holder.llBg.setBackgroundResource(R.color.nomalbg_color);
                }else {
                    holder.llBg.setBackgroundResource(R.color.white);
                }
            }
            holder.tvRank.setText(mRankList.get(position).getRowIndex() + "");
            holder.tvPerson.setText(mRankList.get(position).getName());
            holder.tvType.setText(mRankList.get(position).getCounts() + "");
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.tv_rank)
        TextView tvRank;
        @BindView(R.id.tv_person)
        TextView tvPerson;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.ll_bg)
        LinearLayout llBg;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
